define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'government/government/index' + location.search,
                    add_url: 'government/government/add',
                    edit_url: 'government/government/edit',
                    del_url: 'government/government/del',
                    multi_url: 'government/government/multi',
                    import_url: 'government/government/import',
                    table: 'government_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'government_id',
                sortName: 'government_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'government_id', title: __('ID')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'province_text', title: __('省份')},
                        {field: 'city_text', title: __('市')},
                        {
                            field: 'district_text', class: 'autocontent', title: __('区'), formatter: function (row) {
                                let text = '';
                                row.map(item => {
                                    text += item.name + ',';
                                });
                                return text;
                            }
                        },
                        {
                            field: 'created_at',
                            title: __('创建时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updated_at',
                            title: __('Updated_at'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-city").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {pid: $("#c-province").val()}};
            });
            $("#c-district").data("params", function (obj) {
                return {custom: {pid: $("#c-city").val()}};
            });
            $(document).on("change", "#c-province", function () {
                $("#c-city").selectPageClear();
            });
            $(document).on("change", "#c-city", function () {
                $("#c-district").selectPageClear();
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-city").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {pid: $("#c-province").val()}};
            });
            $("#c-district").data("params", function (obj) {
                return {custom: {pid: $("#c-city").val()}};
            });
            $(document).on("change", "#c-province", function () {
                $("#c-city").selectPageClear();
            });
            $(document).on("change", "#c-city", function () {
                $("#c-district").selectPageClear();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
